<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">

          <el-form-item label="活动名称">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="活动类型">
            <el-radio v-for="item in dict.huodong_type" :key="item.id" v-model="form.type" :label="item.value">{{ item.label }}</el-radio>
          </el-form-item>
          <el-form-item label="场地地址" prop="address">
            <el-input v-model="form.address" style="width: 370px;" />
            <el-button  size="medium" type="primary" @click="getL(form.address)">获取经纬度</el-button>
          </el-form-item>
          <el-form-item label="纬度">
            <el-input v-model="form.latitude" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="经度">
            <el-input v-model="form.longitude" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="活动开始时间">
            <el-date-picker v-model="form.startTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="活动结束时间">
            <el-date-picker v-model="form.endTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="是否显示" prop="isShow">
            <el-radio v-for="(item, index) in dict.is_enable" :key="index" v-model="form.isShow" :label="item.value">{{ item.label }}</el-radio>
          </el-form-item>
          <el-form-item label="图片">
              <MaterialList
                      v-model="imgArr"
                      style="width: 915px;"
                      type="image"
                      :num="1"
                      :width="150"
                      :height="150"
              />
          </el-form-item>
          <el-form-item label="活动内容">
            <el-input :rows="3" v-model="form.content" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="活动链接">
            <el-input :rows="3" v-model="form.url" type="textarea" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('id')" prop="id" label="id" />
        <el-table-column v-if="columns.visible('name')" prop="name" label="活动名称" />
        <el-table-column v-if="columns.visible('type')" prop="type" label="活动类型">
          <template slot-scope="scope">
            {{ dict.label.huodong_type[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('latitude')" prop="latitude" label="纬度" />
        <el-table-column v-if="columns.visible('longitude')" prop="longitude" label="经度" />
        <el-table-column v-if="columns.visible('startTime')" prop="startTime" label="活动开始时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('endTime')" prop="endTime" label="活动结束时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="添加时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('isShow')" prop="isShow" label="是否显示">
          <template slot-scope="scope">
            {{ dict.label.is_enable[scope.row.isShow] }}
          </template>
        </el-table-column>
          <el-table-column v-if="columns.visible('img')" prop="img" label="图片">
            <template slot-scope="scope">
              <a :href="scope.row.img" style="color: #42b983" target="_blank">
                <img :src="scope.row.img" alt="点击打开" class="table-img" />
              </a>
            </template>
          </el-table-column>
        <el-table-column v-if="columns.visible('content')" prop="content" label="活动内容" />
        <el-table-column v-if="columns.visible('url')" prop="url" label="活动链接" />
        <el-table-column v-permission="['admin','yxHuodong:edit','yxHuodong:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudYxHuodong from '@/api/yxHuodong'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";
import { getL } from '@/api/yxSystemStore'

// crud交由presenter持有
const defaultCrud = CRUD({ title: 'huodong', url: 'api/yxHuodong', sort: 'id,desc', crudMethod: { ...crudYxHuodong }})
const defaultForm = { id: null, name: null, type: null, latitude: null, longitude: null, startTime: null, endTime: null, createTime: null, updateTime: null, isShow: null, isDel: null, img: null, content: null, url: null }
export default {
  name: 'YxHuodong',
  components: { pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['huodong_type', 'is_enable', 'is_enable'],
  data() {
    return {
      imgArr:[],
      permission: {
        add: ['admin', 'yxHuodong:add'],
        edit: ['admin', 'yxHuodong:edit'],
        del: ['admin', 'yxHuodong:del']
      },
      rules: {
        id: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '添加时间不能为空', trigger: 'blur' }
        ],
        isShow: [
          { required: true, message: '是否显示不能为空', trigger: 'blur' }
        ],
        isDel: [
          { required: true, message: '是否删除不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'name', display_name: '活动名称' }
      ]
    }
  },
  watch: {
    "imgArr":function(val) {
      if (val) {
        this.form.img = val.join(",");
      }
    },
  },
  methods: {
    getL,
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.name
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.img && form.id) {
        this.imgArr = form.img.split(",");
      }else{
        this.imgArr=[]
      }
    },
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
